package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersAdminMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    private Long PASS_TIME = 900000L;

    @Autowired
    OrdersAdminMapper ordersAdminMapper;


    //15分钟超时取消订单
    @Scheduled(cron = "0 * * * * ?")
    public void orderPassTime(){
//查询待支付订单
        List<Orders> orders = ordersAdminMapper.QueryNoPayOrder();

        if(orders == null && orders.size() == 0){return;}
            orders.forEach(s->{
            //订单开始时间
            Timestamp createTime = Timestamp.valueOf(s.getOrderTime());
            //现在时间
            Timestamp nowTime = Timestamp.valueOf(LocalDateTime.now());
            //计算时间
            Long useTime = nowTime.getTime() - createTime.getTime();

            if(useTime > PASS_TIME){
                ordersAdminMapper.updateCancelOrderMS("超时",s.getId(),LocalDateTime.now());
            }

        });
    }


    //超时派送订单
    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点
    public void orderDeliveryPassTime(){
        LocalDateTime now = LocalDateTime.now();

        LocalDateTime time = now.plusHours(-1);

        List<Orders> orders = ordersAdminMapper.QueryDeliveryPassOrder(time,Orders.DELIVERY_IN_PROGRESS);

        if(orders != null && orders.size() > 0){
            orders.forEach(s->{
            ordersAdminMapper.upateOrdercompleteStatus(s.getId(),LocalDateTime.now());
        });}


    }
}
